<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\Message\CreateRequest;
use App\Http\Requests\Admin\Message\UpdateRequest;
use App\Models\Message;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MessageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $messages = Message::get();
        return view('admin.message.index', compact('messages'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.message.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateRequest $request)
    {
        $data = $request->all();
        if (Message::create($data)) {
            return redirect()->to(route('admin.message'))->with(['status' => '添加成功']);
        }
        return redirect()->to(route('admin.message'))->withErrors('系统错误');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $message = Message::findOrFail($id);
        return view('admin.message.edit', compact('message'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        $message = Message::findOrFail($id);
        $data    = $request->all();
        if ($message->update($data)) {
            return redirect()->to(route('admin.message'))->with(['status' => '编辑成功']);
        }
        return redirect()->to(route('admin.message'))->withErrors('系统错误');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Message::destroy($id);
        return redirect()->to(route('admin.message'))->with(['status' => '删除成功']);
    }
}
